package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllRulefields", query = "select myRulefield from Rulefield myRulefield"),
		@NamedQuery(name = "findRulefieldByName", query = "select myRulefield from Rulefield myRulefield where myRulefield.name = ?1"),
		@NamedQuery(name = "findRulefieldByNameContaining", query = "select myRulefield from Rulefield myRulefield where myRulefield.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findRulefieldByEntityclassname", query = "select myRulefield from Rulefield myRulefield where myRulefield.entityclassname = ?1"),
		@NamedQuery(name = "findRulefieldByEntityclassnameContaining", query = "select myRulefield from Rulefield myRulefield where myRulefield.entityclassname like concat('%',?1,'%')"),
		@NamedQuery(name = "findRulefieldByEntityfieldname", query = "select myRulefield from Rulefield myRulefield where myRulefield.entityfieldname = ?1"),
		@NamedQuery(name = "findRulefieldByEntityfieldnameContaining", query = "select myRulefield from Rulefield myRulefield where myRulefield.entityfieldname like concat('%',?1,'%')"),
		@NamedQuery(name = "findRulefieldByDescription", query = "select myRulefield from Rulefield myRulefield where myRulefield.description = ?1"),
		@NamedQuery(name = "findRulefieldByDescriptionContaining", query = "select myRulefield from Rulefield myRulefield where myRulefield.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllRulefieldsByRule", query = "select myRulefield from Rulefield myRulefield where myRulefield.rule.id = ?1"),
		@NamedQuery(name = "findRulefieldById", query = "select myRulefield from Rulefield myRulefield where myRulefield.id = ?1"),
		@NamedQuery(name = "findRulefieldByPrimaryKey", query = "select myRulefield from Rulefield myRulefield where myRulefield.id = ?1 "),
		@NamedQuery(name = "deleteRulefieldsByIds", query = "delete Rulefield where id in (?1)"),
		@NamedQuery(name = "findRulefieldsByIds", query = "select myRulefield from Rulefield myRulefield where myRulefield.id in (?1)"),
		@NamedQuery(name = "findRulefieldByPhyscialPrimaryKey", query = "select myRulefield from Rulefield myRulefield where  myRulefield.rule.id = ?1 and myRulefield.entityclassname = ?2 and myRulefield.entityfieldname = ?3"),
		@NamedQuery(name = "countRulefieldsByName", query = "select count(myRulefield)  from Rulefield myRulefield where myRulefield.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "rulefield")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Rulefield")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Rulefield implements Serializable, Comparable<Rulefield> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "entityclassname")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String entityclassname;

	@Column(name = "entityfieldname")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String entityfieldname;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "rule", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Rule rule;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "rulefield", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Rulecolumn> rulecolumnsForRulefield;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setEntityclassname(String entityclassname) {
		this.entityclassname = entityclassname;
	}

	public String getEntityclassname() {
		return this.entityclassname;
	}

	public void setEntityfieldname(String entityfieldname) {
		this.entityfieldname = entityfieldname;
	}

	public String getEntityfieldname() {
		return this.entityfieldname;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setRule(Rule rule) {
		this.rule = rule;
	}

	public Rule getRule() {
		return rule;
	}

	public void setRulecolumnsForRulefield(Set<Rulecolumn> rulecolumnsForRulefield) {
		this.rulecolumnsForRulefield = rulecolumnsForRulefield;
	}

	public Set<Rulecolumn> getRulecolumnsForRulefield() {
		if (rulecolumnsForRulefield == null) {
			rulecolumnsForRulefield = new java.util.LinkedHashSet<Rulecolumn>();
		}
		return rulecolumnsForRulefield;
	}

	public void copy(Rulefield that) {

		setId(that.getId());

		setName(that.getName());
		setEntityclassname(that.getEntityclassname());
		setEntityfieldname(that.getEntityfieldname());
		setDescription(that.getDescription());

		setRule(that.getRule());

		setRulecolumnsForRulefield(
				new java.util.LinkedHashSet<Rulecolumn>(that.getRulecolumnsForRulefield()));

	}

	public Rulefield() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("entityclassname=[").append(entityclassname).append("] ");
		buffer.append("entityfieldname=[").append(entityfieldname).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Rulefield))
			return false;
		Rulefield comparedRulefield = (Rulefield) obj;
		if (!CheckPropertyEqual(this.rule.id, comparedRulefield.rule.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.entityclassname, comparedRulefield.entityclassname)) {
			return false;
		}
		if (!CheckPropertyEqual(this.entityfieldname, comparedRulefield.entityfieldname)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Rulefield o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
